<!--折线图-->
<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './../mixins/resize'
  // import { listLine } from "@/api/dashboard/linechart"
  import { listBar } from "@/api/dashboard/barchart";

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '303px'
      },
      autoResize: {
        type: Boolean,
        default: true
      },
    },
    data() {
      return {
        chart: null,
        MapList:[],
        rows:[],
        LineList:[],
        barInAmount:[],
        barOutAmount:[],
        barDate :[],
        queryParams: {
          barInAmount: undefined,
          barOutAmount: undefined,
          barDate: undefined,
        },
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    created() {
      this.getLineList();
    },
    methods: {
      getLineList(){
        this.loading = true;
        listBar(this.queryParams).then(response => {
          this.MapList = response.rows;
          console.log( "折线图数据：" + this.MapList);
          let MapList = this.MapList;
          if(MapList) {
            let obj = eval(MapList);      //eval() 函数可计算某个字符串，并执行其中的的 JavaScript 代码。返回值是通过计算 string 而得到的值
            for (let i = 0; i < obj.length; i++) {
              this.barInAmount.push(MapList[i].barInCount);
              this.barOutAmount.push(MapList[i].barOutCount);
              this.barDate.push(MapList[i].barDate);
            }
          }
          this.chart.setOption({
            xAxis: {
              data: this.barDate
            },
            series: [{
              name: '入库金额',
              data: this.barInAmount,
            },
              {
                name: '出库金额',
                data: this.barOutAmount,
              }]
          });
        });
      },
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.chart.setOption({
          xAxis: {
            data: this.barDate,
            boundaryGap: false,
            axisTick: {
              show: false
            }
          },
          grid: {
            left: 10,
            right: 10,
            bottom: 20,
            top: 30,
            containLabel: true
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross'
            },
            padding: [5, 10]
          },
          yAxis: {
            axisTick: {
              show: false
            }
          },
          legend: {
            data: ['入库金额', '出库金额'],
            textStyle:{
              color: 'black'
            }
          },
          series: [
            {
            name: '入库金额',
              itemStyle: {
              normal: {
                color: '#FF005A',
                lineStyle: {
                  color: '#FF005A',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data:  this.barInAmount,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
            {
              name: '出库金额',
              smooth: true,
              type: 'line',
              itemStyle: {
                normal: {
                  color: '#3888fa',
                  lineStyle: {
                    color: '#45E4A5',
                    width: 2
                  },
                }
              },
              data: this.barOutAmount,
              animationDuration: 2800,
              animationEasing: 'quadraticOut'
            }]
        })
      },
    }
  }
</script>
